using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using System.Linq.Expressions;
using ACME.Northwind.Service.Interfaces;

namespace ACME.Northwind.WCFProxy
{
	/// <summary>
	/// The service implementation for Order object
	/// </summary>
	public partial class OrderService : IOrderService
	{
		#region IOrderService Members

		/// <summary>
		/// Run a selection by the primary key
		/// </summary>
		public OrderDTO SelectByPrimaryKey(int orderid)
		{
			return DataService.Instance.GetOrder(orderid);
		}

		/// <summary>
		/// Run the persist for a list
		/// </summary>
		public bool Persist(List<OrderDTO> list)
		{
			bool retval = true;
			foreach (OrderDTO order in list)
			{
				retval &= this.Persist(order);
			}
			return retval;
		}

		/// <summary>
		/// Run the persist for an object
		/// </summary>
		public bool Persist(OrderDTO dto)
		{
			return (DataService.Instance.PersistOrder(dto) != null);
		}

		/// <summary>
		/// Run a selection using LINQ
		/// </summary>
		public List<OrderDTO> RunSelect(string linq)
		{
			List<OrderDTO> retVal = new List<OrderDTO>();
			retVal.AddRange(DataService.Instance.GetOrderListByLINQ(linq));
			return retVal;
		}

		/// <summary>
		/// Run a paginated selection using LINQ
		/// </summary>
		public PagedQueryResults<OrderDTO> RunSelect(int pageIndex, int pageSize, bool ascending, string sortColumn, string linq)
		{
			return DataService.Instance.GetOrderListPaged(pageIndex, pageSize, ascending, sortColumn, linq);
		}

		/// <summary>
		/// Run a selection for all objects
		/// </summary>
		public List<OrderDTO> RunSelect()
		{
			List<OrderDTO> retVal = new List<OrderDTO>();
			DataServiceClient ds = DataService.Instance;
			retVal.AddRange(ds.GetOrderList());
			return retVal;
		}

		/// <summary>
		/// Return a list of related OrderDetail objects
		/// </summary>
		public List<OrderDetailDTO> GetOrderDetailList(OrderDTO item)
		{
			List<OrderDetailDTO> retval = new List<OrderDetailDTO>();
			DataServiceClient ds = DataService.Instance;
			string s = "";
			s += "x.OrderId = " + item.OrderId + "";
			retval.AddRange(ds.GetOrderDetailListByLINQ("x => " + s));
			return retval;
		}

		/// <summary>
		/// Return the related Customer item
		/// </summary>
		public CustomerDTO GetCustomerItem(OrderDTO item)
		{
			CustomerDTO retval = new CustomerDTO();
			if (false ||
				(item.CustomerId == null) ||
				false
			)
			{
				//The primary key cannot be null, so there is no object
				return null;
			}
			DataServiceClient ds = DataService.Instance;
			retval = ds.GetCustomer(item.CustomerId);
			return retval;
		}

		/// <summary>
		/// Return the related Employee item
		/// </summary>
		public EmployeeDTO GetEmployeeItem(OrderDTO item)
		{
			EmployeeDTO retval = new EmployeeDTO();
			if (false ||
				(item.EmployeeId == null) ||
				false
			)
			{
				//The primary key cannot be null, so there is no object
				return null;
			}
			DataServiceClient ds = DataService.Instance;
			retval = ds.GetEmployee(item.EmployeeId.Value);
			return retval;
		}

		/// <summary>
		/// Return the related Shipper item
		/// </summary>
		public ShipperDTO GetShipperItem(OrderDTO item)
		{
			ShipperDTO retval = new ShipperDTO();
			if (false ||
				(item.ShipVia == null) ||
				false
			)
			{
				//The primary key cannot be null, so there is no object
				return null;
			}
			DataServiceClient ds = DataService.Instance;
			retval = ds.GetShipper(item.ShipVia.Value);
			return retval;
		}

		/// <summary>
		/// Delete the specified item
		/// </summary>
		public bool Delete(OrderDTO item)
		{
			DataServiceClient ds = DataService.Instance;
			return ds.DeleteOrder(item);
		}

		#endregion
	}
}
